import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from 'vant'
import {getToken} from '@/utils/storage'
// 一级路由
import Login from '@/views/login.vue'
import Register from '@/views/register.vue'
import Detail from '@/views/detail.vue'
import Layout from '@/views/layout.vue'
import  Map  from '@/views/map.vue'
import Taxi from '@/views/taxi.vue'

// 二级路由
import My from '@/views/my.vue'
import Article from '@/views/article.vue'
import Information from '@/views/information.vue'
import Search from '@/views/search.vue'
import Collect from '@/views/collect.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/taxi',
    component: Taxi
  },
  {
    path: '/collect',
    component: Collect
  },
  {
    path: '/map',
    component: Map
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/article/:id',
    component: Detail
  },
  {
    path: '/',
    component: Layout,
    redirect: '/article',
    children: [
      { path: '/article', component: Article },
      { path: '/my', component: My },
      { path: '/information', component: Information },
      { path: '/search', component: Search }
    ]
  }
]

const router = new VueRouter({
  routes
})
//全局前置守卫
const whiteList = ['/login', '/register',]

router.beforeEach((to, from, next) => {
  if (getToken()) {
    next()
  } else {
    if (whiteList.includes(to.path)) {
      next()
    } else {
      Toast.fail('希望登录一下')
      next('/login')
    }
  }
})

export default router
